package com.rocket.activity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rocket.R;
import com.rocket.entity.FirearmType;
import com.rocket.entity.Work;
import com.rocket.excel.WorkExcelUtil;
import com.rocket.util.DialogUtil;
import com.rocket.util.MenuUtil;

public class WorkInfoActivity extends MenuActivity {

	Button			buttonBack;
	Button			buttonSave;
	Button			buttonTu;
	TextView		tvFirearmType;
	TextView		tvName;
	TextView		tvNumber;
	TextView		tvFixedLon;
	TextView		tvFixedLat;
	TextView		tvMoveLon;
	TextView		tvMoveLat;
	TextView		tvHaiba;
	TextView		tvCloudType;
	TextView		tvCloudTop;
	TextView		tvCloudBottom;
	TextView		tvCenterHeight;
	TextView		tvCloud6Height;
	LinearLayout	layoutRocket;
	TextView		tvRocketAngle;
	TextView		tvRocketHeight;
	TextView		tvRocketLength;
	LinearLayout	layoutMissile;
	TextView		tvMissileAngle;
	TextView		tvMissileTime;
	TextView		tvMissileHeight;
	TextView		tvMissileLength;
	TextView		tvCreateTime;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.workinfo);

		final Work work = (Work) getIntent().getSerializableExtra("work");

		int firearmType = work.firearmType;

		buttonBack = (Button) findViewById(R.id.buttonback);
		buttonBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

		buttonSave = (Button) findViewById(R.id.buttonsave);
		buttonSave.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Rocket/work_" + System.currentTimeMillis() + ".xls";
				ArrayList<Work> works = new ArrayList<Work>();
				works.add(work);
				WorkExcelUtil.getInstance().export(works, path);
				showToast("Excel文件保存成功，文件路径为：" + path);
			}
		});
		buttonTu = (Button) findViewById(R.id.buttontubiao);
		buttonTu.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(WorkInfoActivity.this, TubiaoActivity.class);
				intent.putExtra("work", work);
				startActivity(intent);
			}
		});

		tvFirearmType = (TextView) findViewById(R.id.tvtype);
		tvName = (TextView) findViewById(R.id.tvname);
		tvName.setText(work.name);
		tvNumber = (TextView) findViewById(R.id.tvnumber);
		tvNumber.setText(work.number);
		tvFixedLon = (TextView) findViewById(R.id.tvfixedlon);
		tvFixedLon.setText(String.valueOf(work.fixedLongitude));
		tvFixedLat = (TextView) findViewById(R.id.tvfixedlat);
		tvFixedLat.setText(String.valueOf(work.fixedLatitude));
		tvMoveLon = (TextView) findViewById(R.id.tvmovelon);
		tvMoveLon.setText(String.valueOf(work.moveLongitude));
		tvMoveLat = (TextView) findViewById(R.id.tvmovelat);
		tvMoveLat.setText(String.valueOf(work.moveLatitude));
		tvHaiba = (TextView) findViewById(R.id.tvhaiba);
		tvHaiba.setText(String.valueOf(work.haiba));
		tvCloudType = (TextView) findViewById(R.id.tvcloudtype);
		tvCloudType.setText(work.cloudType);
		tvCloudTop = (TextView) findViewById(R.id.tvcouldtop);
		tvCloudTop.setText(String.valueOf(work.cloudTopHeight));
		tvCloudBottom = (TextView) findViewById(R.id.tvcloudbottom);
		tvCloudBottom.setText(String.valueOf(work.cloudBottomHeight));
		tvCenterHeight = (TextView) findViewById(R.id.tvcloudcenter);
		tvCenterHeight.setText(String.valueOf(work.cloudCenterHeight));
		tvCloud6Height = (TextView) findViewById(R.id.tvcloud6height);
		tvCloud6Height.setText(String.valueOf(work.cloud6Height));
		layoutRocket = (LinearLayout) findViewById(R.id.layoutrocket);
		tvRocketAngle = (TextView) findViewById(R.id.tvrocketangle);
		tvRocketAngle.setText(String.valueOf(work.rocketAngle));
		tvRocketHeight = (TextView) findViewById(R.id.tvrocketheight);
		tvRocketHeight.setText(String.valueOf(work.rocketMaxHeight));
		tvRocketLength = (TextView) findViewById(R.id.tvrocketlength);
		tvRocketLength.setText(String.valueOf(work.rocketMaxLength));
		layoutMissile = (LinearLayout) findViewById(R.id.layoutmissile);
		tvMissileAngle = (TextView) findViewById(R.id.tvmissileangle);
		tvMissileAngle.setText(String.valueOf(work.missileAngle));
		tvMissileTime = (TextView) findViewById(R.id.tvmissiletime);
		tvMissileTime.setText(String.valueOf(work.misseleTime));
		tvMissileHeight = (TextView) findViewById(R.id.tvmissileheight);
		tvMissileHeight.setText(String.valueOf(work.missileMaxHeight));
		tvMissileLength = (TextView) findViewById(R.id.tvmissilelength);
		tvMissileLength.setText(String.valueOf(work.missileMaxLength));
		tvCreateTime = (TextView) findViewById(R.id.tvcreatetime);

		SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
		tvCreateTime.setText(format.format(new Date(work.createTime)));

		switch (firearmType) {
			case FirearmType.ROCKET_BL1:
				tvFirearmType.setText("BL-1型火箭弹");
				layoutMissile.setVisibility(View.GONE);
				break;
			case FirearmType.ROCKET_HJD82:
				tvFirearmType.setText("HJD-82型火箭弹");
				layoutMissile.setVisibility(View.GONE);
				break;
			case FirearmType.ROCKET_RY6300:
				tvFirearmType.setText("RYI-6300型火箭弹");
				layoutMissile.setVisibility(View.GONE);
				break;
			case FirearmType.ROCKET_WR98:
				tvFirearmType.setText("WR-98型火箭弹");
				layoutMissile.setVisibility(View.GONE);
				break;
			case FirearmType.MISSILE_83:
				tvFirearmType.setText("83型炮弹");
				layoutRocket.setVisibility(View.GONE);
				break;
			case FirearmType.MISSILE_JD89:
				tvFirearmType.setText("JD-89型炮弹");
				layoutRocket.setVisibility(View.GONE);
				break;
			default:
				layoutMissile.setVisibility(View.GONE);
				layoutRocket.setVisibility(View.GONE);
				break;
		}
	}

	@Override
	protected void openMenu() {
		String[] texts = new String[] { "火器选择", "添加站点", "历史查询", "退出程序" };
		Integer[] images = new Integer[] { R.drawable.menu_logout,
				R.drawable.menu_sys_add_buddy,
				R.drawable.menu_sys_search_buddy, R.drawable.menu_icon };
		mMenu = new MenuUtil(WorkInfoActivity.this, getLayoutInflater()
				.inflate(R.layout.workinfo, null), texts, images);
		mMenu.setMenuItemSelectedLinstener(new OnClickListener() {

			public void onClick(View v) {
				int tag = (Integer) v.getTag();
				switch (tag) {
					case 0:
					mMenu.dismiss();
					Intent intent1 = new Intent(WorkInfoActivity.this,
							ChoseFirearmActivity.class);
					intent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(intent1);
					break;
				case 1:
					mMenu.dismiss();
					Intent intent2 = new Intent(WorkInfoActivity.this,
							WorkStationAddActivity.class);
					intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(intent2);
					break;
				case 2:
					mMenu.dismiss();
					Intent intent3 = new Intent(WorkInfoActivity.this,
							HistoryQueryActivity.class);
					intent3.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(intent3);
					break;
				case 3:
					mMenu.dismiss();
					mDialog = new DialogUtil(WorkInfoActivity.this);
					mDialog.setTitle("退出确认");
					mDialog.setMessage("确定退出吗？");
					mDialog.setPositiveButton("确定", new OnClickListener() {

						public void onClick(View v) {
							mDialog.dismiss();
							Intent intent = new Intent(WorkInfoActivity.this,
									MainActivity.class);
							intent.putExtra("Flag", 1);
							intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
							startActivity(intent);
						}
					});
					mDialog.setNegativeButton("取消", null);
					mDialog.show();
					break;
				default:
					break;
			}
		}
		});
		mMenu.show();
	}

}
